#include "config.h"
#include "JA_Protocol.h"
#include "user_usart.h"
#include "malloc.h"
#include "Model_Info.h"
#include "user_usart.h"
extern Mallco_dev mallco_dev; //内存管理器
JA_Protocol_TCB ja_tcb; // JA 协议控制块
#define JA_Protocol_MAX_SIZE 20
uint8_t JA_Protocol_BUF[JA_Protocol_MAX_SIZE]; // JA 协议缓存


/// @brief 获取RF 默认参数
/// @param  
/// @return 
Lora_RFConfig_t GetDefaultRFConfig(void)
{
	Lora_RFConfig_t lora_config;
	lora_config.RF_power=22;
	lora_config.RF_RXFrequency=450000000;
	lora_config.RF_TXFrequency=450000000;
	lora_config.ErrorCoding=LoRa_ErrorCoding_4_5;
	lora_config.SignalBw=LoRa_SignalBw_125K;
	lora_config.SpreadingFactor=LoRa_SpreadingFactor_512;
	return lora_config;
}
/// @brief 保存配置参数
/// @param  net 配置
/// @return 
void Save_Config(void)
{
	Clear_Info();
	Save_ConfigInfo(&model_info_handle.general_info);
}

void Set_Info(uint32_t id)
{
	model_info_handle.general_info.SN=id;
}
/// @brief 配置回调函数
/// @param data 数据
/// @param Size 数据长度
void Config_RxCallback(uint8_t *data, uint8_t Size)
{
	JA_BytesArray reply_buf;
	JA_StatusTypeDef res;
	for(uint8_t i=0;i<Size;i++)
	{
		res = JA_Decode(&ja_tcb, data[i], &reply_buf);
		if (res == JA_Decode_OK)
		{
			if (reply_buf.len)
			{
				UART1Send_Data(reply_buf.bytes, reply_buf.len);
				myfree(&mallco_dev, reply_buf.bytes);
			}
		}
	}
}




/// @brief DTU初始化
/// @param  
void Init_Model(void)
{
	Init_Modelinfo();
	JA_Create(&ja_tcb, DTU_Config_Parse, JA_Protocol_BUF, JA_Protocol_MAX_SIZE);
}


/// @brief 协议回复数据
/// @param protocol 协议
/// @return 回复的数据
JA_BytesArray Reply_Data(JA_Protocol protocol)
{
	JA_BytesArray buf;
	buf.bytes = mymalloc(&mallco_dev, protocol.data.data_len + 8);
	if (buf.bytes)
	{
		buf.len = JA_Package(protocol, buf.bytes);
		return buf;
	}
}

/// @brief 回复状态
/// @param protocol 协议 
/// @param status 状态
/// @return 
JA_BytesArray Reply_status(JA_Protocol protocol,uint8_t status)
{
	JA_Protocol r_protocol;
	r_protocol.Address=protocol.Address;
	r_protocol.CS_Vsersion=protocol.CS_Vsersion;
	r_protocol.SN=protocol.SN;
	r_protocol.data.Opcode=protocol.data.Opcode;
	r_protocol.data.data=&status;
	r_protocol.data.data_len=1;
	return Reply_Data(r_protocol);
}


/// @brief 查询信息命令解析
/// @param protocol 协议
/// @return 查询参数
JA_BytesArray Get_Info_Parse(JA_Protocol protocol)
{
	JA_Protocol r_protocol;
	Command_info_t info;
	info.software_version.major=APP_VERSION_Major;
	info.software_version.minor=App_VERSION_Minor;
	info.software_version.revision=APP_VERSION_Revision;
	info.software_version.build=APP_VERSION_Build;
	info.solidify=model_info_handle.general_info;
	info.dev_type=model_info_handle.model_type;
	//mymemcpy((uint8_t*)&r_protocol.data.data,&info,sizeof(Command_info_t));
	r_protocol.data.data=(uint8_t *)&info;
	r_protocol.Address=protocol.Address;
	r_protocol.CS_Vsersion=protocol.CS_Vsersion;
	r_protocol.SN=protocol.SN;
	r_protocol.data.Opcode=protocol.data.Opcode;
	r_protocol.data.data_len=sizeof(Command_info_t);
	return Reply_Data(r_protocol);
}

//设置出厂信息
JA_BytesArray Set_Info_Parse(JA_Protocol protocol)
{
	JA_Protocol r_protocol;
	Command_SetInfo_t info;
	mymemcpy((uint8_t*)&info,protocol.data.data,sizeof(Command_SetInfo_t));
	Set_Info(info.id);
	//Set_PortConfig(port_info.rs232_port,port_info.rs485_port);
	r_protocol.Address=protocol.Address;
	r_protocol.CS_Vsersion=protocol.CS_Vsersion;
	r_protocol.SN=protocol.SN;
	r_protocol.data.Opcode=protocol.data.Opcode;
	return Reply_status(r_protocol,1);
}
/// @brief 保存参数解析
/// @param protocol 协议
/// @return 保存状态
JA_BytesArray Save_Parse(JA_Protocol protocol)
{
	JA_Protocol r_protocol;
	Save_Config();
	r_protocol.Address=protocol.Address;
	r_protocol.CS_Vsersion=protocol.CS_Vsersion;
	r_protocol.SN=protocol.SN;
	r_protocol.data.Opcode=protocol.data.Opcode;
	return Reply_status(r_protocol,1);
}
/// @brief DTU 配置协议解析
/// @param protocol 协议
/// @param command_buf 命令buf
/// @return 协议回复
JA_BytesArray  DTU_Config_Parse(JA_Protocol protocol, uint8_t *command_buf)
{
	JA_BytesArray reply_buf;
	reply_buf.len = 0;
	reply_buf.bytes = NULL;
	
	switch (protocol.data.Opcode)
	{
	case (uint8_t)ConfigCommand_SaveConfig:       //保存配置参数
		reply_buf=Save_Parse(protocol);
		break;	
	case (uint8_t)ConfigCommand_GetInfo:          //查询固化信息
		reply_buf=Get_Info_Parse(protocol);
		break;   
	case (uint8_t)ConfigCommand_SetInfo:         //设置出厂信息
		reply_buf=Set_Info_Parse(protocol);
	  break;
	default:
		break;
	}
	return reply_buf;
}
